package Hxw3DEngine.asset.parsers
{
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	public class ParserBase extends EventDispatcher
	{
		public var targetData:ByteArray;
		public var frameLimit:Number;
		public var lastFrameTime:Number;
		private var timer:Timer;
		//是否正在解析
		public var startedParsing:Boolean;
		
		protected var error:Error;
		
		public function startParser(_data:ByteArray,_frameLimit:Number = 30):void
		{
			targetData = _data;
			//targetData.endian = Endian.LITTLE_ENDIAN;
			frameLimit = _frameLimit;
			timer = new Timer(frameLimit, 0);
			timer.addEventListener(TimerEvent.TIMER, onInterval);
			timer.start();
		}
		
		public function proceedParsing():Boolean
		{
			return false;
		}
		
		public function hasTime():Boolean
		{
			return true;
			var now:Number = getTimer();
			return ((now - lastFrameTime) < frameLimit);
		}
		
		private function onInterval(event:TimerEvent = null):void
		{
			lastFrameTime = getTimer();
			if (proceedParsing()) finishParsing();
		}
		
		protected function finishParsing():void
		{
			if (timer) {
				timer.removeEventListener(TimerEvent.TIMER, onInterval);
				timer.stop();
			}
			timer = null;
			dispatchEvent(new ParserEvent(error==null?ParserEvent.PARSERCOMPLETE:ParserEvent.PARSERERROR,error));
		}
		
		//------------------------公共取数据的方法-------------------------
		
		
		private var _vertexData:Vector.<Number>;
		
		public function get vertexData():Vector.<Number>{
			return _vertexData ||= new Vector.<Number>;
		}
		
		private var _uvData:Vector.<Number>;
		
		public function get uvData():Vector.<Number>{
			return _uvData ||= new Vector.<Number>;
		}
		
		private var _indexData:Vector.<uint>;
		
		public function get indexData():Vector.<uint>{
			return _indexData ||= new Vector.<uint>;
		}
		
		private var _normalData:Vector.<Number>;
		public function get normalData():Vector.<Number>{
			return _normalData ||= new Vector.<Number>;
		}
		
		public function set normalData(val:Vector.<Number>):void{
			_normalData = val;
		}
		
		
		private var _tangentData:Vector.<Number>;
		public function get tangentData():Vector.<Number>{
			return _tangentData ||= new Vector.<Number>;
		}
		
	}
}

